package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.bo.DataItemChannelBo;
import cn.broadsense.biz.bo.DataItemChannelQueryBo;
import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.DataItemChannel;
import cn.broadsense.biz.dto.DataItemChannelDto;
import cn.broadsense.biz.service.DataItemChannelService;
import cn.broadsense.biz.vo.ChannelVo;
import cn.broadsense.common.annotation.Log;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfo;
import cn.broadsense.common.core.page.TableDataInfoResult;
import cn.broadsense.common.enums.BusinessType;
import cn.broadsense.common.utils.poi.ExcelUtil;
import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author: pengxin
 * @create: 2024-09-11 11:49
 * @Version 1.0
 **/
@RestController
@Api(tags = "数据接入点位")
@RequestMapping("/dataItemChannel/")
@RequiredArgsConstructor
public class DataItemChannelController extends BaseController {

    private final DataItemChannelService dataItemChannelService;


    @ApiOperation(value = "获取数据未接入点位列表")
    @GetMapping("/channelList")
    public TableDataInfoResult<List<Channel>> channelList(String keywords, Integer cameraType, Date start, Date end) {
        List<Channel> channelList = dataItemChannelService.listChannel(keywords, cameraType, start, end);
        return getDataTableResult(channelList);
    }


    @ApiOperation(value = "获取数据接入点位列表")
    @GetMapping("/list")
    public TableDataInfoResult<List<DataItemChannel>> list(DataItemChannelQueryBo queryBo) {
        List<DataItemChannel> dataItemChannelList = dataItemChannelService.findList(queryBo);
        return getDataTableResult(dataItemChannelList);
    }

    @ApiOperation(value = "保存数据接入点位列表")
    @Log(title = "数据接入-保存数据", businessType = BusinessType.INSERT)
    @PostMapping("/saveChannelList/{ids}")
    public R<Void> saveChannelList(@PathVariable Long[] ids) {
        dataItemChannelService.saveChannelList(CollUtil.toList(ids));
        return R.ok();
    }

    @ApiOperation(value = "删除数据接入点位列表")
    @DeleteMapping("/remove/{ids}")
    @Log(title = "数据接入-删除数据接入点位", businessType = BusinessType.INSERT)
    public R<Void> remove(@PathVariable Long[] ids) {
        dataItemChannelService.batchRemove(CollUtil.newArrayList(ids));
        return R.ok();
    }

    @ApiOperation(value = "获取数据接入点位详情")
    @GetMapping("/{id}")
    public R<DataItemChannelDto> getDetails(@PathVariable Long id) {
        return R.ok(dataItemChannelService.getDetails(id));
    }

    @PostMapping("/import")
    @ApiOperation("导入点位")
    @Log(title = "数据接入-导入点位", businessType = BusinessType.INSERT)
    public R<Void> importData(MultipartFile file) throws Exception {
        ExcelUtil<DataItemChannelBo> util = new ExcelUtil<DataItemChannelBo>(DataItemChannelBo.class);
        List<DataItemChannelBo> list = util.importExcel(file.getInputStream());
        dataItemChannelService.importChannelList(list);
        return R.ok();
    }

    @ApiOperation("导入模板下载")
    @GetMapping("/import/template")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<DataItemChannelBo> util = new ExcelUtil<DataItemChannelBo>(DataItemChannelBo.class);
        util.importTemplateExcel(response, "导入模板数据");
    }

    @ApiOperation("同步数据")
    @PostMapping("/syncData/{ids}")
    @Log(title = "数据接入-数据同步", businessType = BusinessType.INSERT)
    public R<Void> batchSyncData(@PathVariable Long[] ids) {
        dataItemChannelService.batchSyncData(CollUtil.newArrayList(ids));
        return R.ok();
    }

}
